/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

export class IntervalTimer {
  constructor() {
    this.intervalId = null;
    this.counter = 0;
    this.lastValue = null;
  }

  setBehavior(behaviorType) {
    this.behaviorType = behaviorType;
  }

  start(intervalMs) {
    this.intervalId = setInterval(() => {
      if (this.behaviorType === 'increment') {
        this.counter++;
      } else if (this.behaviorType === 'collect') {
        this.lastValue = `Tick-${Date.now()}`;
      }
    }, intervalMs);
  }

  getCount() {
    return counter;
  }

  getLastValue() {
    return lastValue;
  }

  stop() {
    if (this.intervalId) {
      clearInterval(this.intervalId);
      this.intervalId = null;
    }
  }
}

export class TimerWrapper {
  constructor() {
    this.callback = null;
    this.timeoutId = null;
  }

  setCallback(callbackName) {
    this.callbackName = callbackName;
  }

  start(delay) {
    this.timeoutId = setTimeout(() => {
      if (this.callbackName === 'logMessage') {
        console.log('Timeout triggered');
      } else if (this.callbackName === 'incrementCounter') {
        if (typeof this.counter === 'undefined') {
          this.counter = 0;
        }
        this.counter++;
      }
    }, delay);
  }

  getResult() {
    return this.callbackName === 'incrementCounter' ? this.counter : null;
  }

  clear() {
    if (this.timeoutId) {
      clearTimeout(this.timeoutId);
      this.timeoutId = null;
    }
  }
}

export let p = new Promise((resolve) => {
  setTimeout(() => resolve("Promise resolved"), 1000);
});

export async function handlePromise(promiseFunc) {
  return await promiseFunc();
}

export function createDelayedPromise(delay, value) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(value), delay);
  });
}

const topLevelResult = await createDelayedPromise(500, "Top level await");

export function getTopLevelResult() {
  return topLevelResult;
}

export async function foo() {
  return 1;
}

export function promiseWithFinally() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("resolved"), 500);
  }).finally(() => {
    console.log("[JS] finally callback executed");
    return "finally value";
  });
}

export const fetchData = new Promise((resolve, reject) => {
  setTimeout(() => {
    const data = 'Hello World!';
    resolve(data);
  }, 1000);
});

export function createSharedBuffer() {
  const buffer = new SharedArrayBuffer(16);
  const uint8 = new Uint8Array(buffer);
  uint8[0] = 7;
  return { buffer, uint8 };
}

export function atomicOperations(uint8Array) {
  const originalValue = Atomics.add(uint8Array, 0, 2);
  const newValue = Atomics.load(uint8Array, 0);
  return `originalValue: ${originalValue}, newValue: ${newValue}`;
}